package com.yashi.springmvcjbpm.service.impl;

import org.drools.core.impl.StatefulKnowledgeSessionImpl;
import org.kie.api.KieBase;
import org.kie.api.runtime.KieSession;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.yashi.springmvcjbpm.entity.Leave;

public class RuleServiceimpl {
	
	public static String changeVale(String leaveLong) {
		if(leaveLong.contains("天")) {
			int index = leaveLong.indexOf("天");
			leaveLong=leaveLong.substring(0, index)+"days";
			System.out.println("leaveLong="+leaveLong);
		}else if(leaveLong.contains("周")) {
			int index = leaveLong.indexOf("周");
			int leavedays = Integer.valueOf(leaveLong.substring(0, index));
			leaveLong=String.valueOf(leavedays)+"weeks";
			System.out.println("leaveLong="+leaveLong);
		}
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
				"com/yashi/springmvcjbpm/ruleTest/drools-spring.xml");
		StatefulKnowledgeSessionImpl impl = (StatefulKnowledgeSessionImpl) context.getBean("ksession");
		KieBase kbase = impl.getKieBase();
		KieSession ksession = kbase.newKieSession();
		
		Leave leave = new Leave();
		leave.setLeaveLong(leaveLong);		
		ksession.insert(leave);	
		ksession.fireAllRules();
		return leave.getLeaveLong();
		
	}
	public static void main(String [] args) {
		String leaveLong="1周";
		changeVale(leaveLong);
	}
}
